home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 12.1 KB | 424 lines | [TEXT/MPS ] |
- /*
- File: RegisteredObjectsWindow.cp
-
- Contains: Implementation of TRegisteredObjectsWindow. A window that displays a list
- of TRegisteredObjects.
-
- Copyright: © 1991-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
- #ifndef __STRING__
- #include <string.h>
- #endif
- #ifndef __Resources__
- #include <Resources.h>
- #endif
- #ifndef __STRINGS__
- #include <Strings.h>
- #endif
-
- #ifndef __REGISTEREDOBJECTSWINDOW__
- #include "RegisteredObjectsWindow.h"
- #endif
- #ifndef __REGISTEREDOBJECTS__
- #include "RegisteredObjects.h"
- #endif
- #ifndef __INSPECTOR__
- #include "Inspector.h"
- #endif
- #ifndef __LIST__
- #include "List.h"
- #endif
-
- //
- // structure for saving the location of a window as a resource
- //
- struct WindowLocationRec
- {
- signed short left, top;
- signed short width, height;
- };
-
- /**********************************************************************
- ** PUBLIC Constructor/Destructor
- ***********************************************************************/
-
- TRegisteredObjectsWindow::TRegisteredObjectsWindow()
- {
- fList = NULL;
- fRegisteredObjects = NULL;
- fInspector = NULL;
- }
-
- TRegisteredObjectsWindow::TRegisteredObjectsWindow(short resID,
- TRegisteredObjects *theObjects, TInspector* theInspector) : TDocument(resID)
- {
- InitRegisteredObjectsWindow(theObjects, theInspector);
- }
-
- TRegisteredObjectsWindow::~TRegisteredObjectsWindow()
- {
- fRegisteredObjects->SetRegisteredObjectsWindow(NULL); /* we no longer exist */
- HideWindow(fDocWindow);
- delete fList;
- }
-
- /**********************************************************************
- ** PRIVATE InitRegisteredObjectsWindow
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::InitRegisteredObjectsWindow(TRegisteredObjects *theObjects,
- TInspector* theInspector )
- {
- fInspector = theInspector;
-
- GrafPtr savedPort;
- GetPort(&savedPort);
- SetPort(fDocWindow);
- TextFont(monaco);
-
- /* set the windows title */
-
- char theName[256];
- strcpy(&theName[1],theObjects->GetClassName());
- theName[0] = strlen(&theName[1]);
- SetWTitle(GetDocWindow(),StringPtr(theName));
-
- /* setup the List Manager list */
-
- Rect listRect = fDocWindow->portRect;
- listRect.right -= 15; // scroll bar space
- listRect.bottom -= 15; // scroll bar space
- listRect.left += kButtonSpace; // button space
- Rect dataBounds = {0,0,0,1}; // empty 1 column list
- Point cSize = {0,0}; // let list manager decide cell size
- fList = new TList(&listRect,&dataBounds,cSize,0,fDocWindow,
- true/*drawit*/, true/*hasgrow*/, false/*hScroll*/, true/*vScroll*/);
- fList->SetSelFlags(lOnlyOne); // only allow one cell to be selected
- (**fList->GetList()).cellSize.h = 16383;
-
- fRegisteredObjects = theObjects;
- theObjects->SetRegisteredObjectsWindow(this); /* it needs a pointer to us */
- UpdateList();
-
- SetPort(savedPort);
- }
-
- /**********************************************************************
- ** PUBLIC DoContent
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::DoContent(EventRecord* theEvent)
- {
- //Trace("TRegisteredObjectsWindow::DoContent\n");
- Point mouse;
-
- SetPort(fDocWindow);
- mouse = theEvent->where; // get the click position
- GlobalToLocal(&mouse);
- Rect listRect = GetListRect();
- listRect.right += 15; // 15 for the scrollbars
- listRect.bottom += 15; // 15 for the scrollbars
- if (PtInRect(mouse,&listRect)) { // did we click in list regions?
- if (fList->Click(mouse,theEvent->modifiers)) { // did we double click?
- Cell theCell = fList->LastClick(); // what cell did we double click in
- //*fRegisteredObjects[theCell.v]->Dump(); // dump the object
- TDynamic* theObj = (*fRegisteredObjects)[theCell.v];
- //delete theObj;
- }
- }
- }
-
- /**********************************************************************
- ** PUBLIC DoGrow
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::DoGrow(EventRecord* theEvent)
- {
- Rect sizeRect = {100,175, 32767, 32767};
- Rect oldRect = fDocWindow->portRect;
- long result = GrowWindow(fDocWindow, theEvent->where, &sizeRect);
- if (result)
- {
- short h = LoWord(result);
- short v = HiWord(result);
- SizeWindow(h, v);
- }
- }
-
- /**********************************************************************
- ** PUBLIC DoActivate
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::DoActivate(Boolean becomingActive)
- {
- DrawGrowIcon();
- fList->Activate(becomingActive);
- }
-
- /**********************************************************************
- ** PUBLIC DoUpdate
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::DoUpdate()
- {
- BeginUpdate(fDocWindow); // this sets up the visRgn
- if (!EmptyRgn(fDocWindow->visRgn)) { // draw if updating needs to be done
- fList->Update(fDocWindow->visRgn);
- }
- DrawGrowIcon();
- MoveTo(fDocWindow->portRect.left + kButtonSpace - 1, fDocWindow->portRect.top);
- LineTo(fDocWindow->portRect.left + kButtonSpace - 1, fDocWindow->portRect.bottom);
- EndUpdate(fDocWindow);
- }
-
- /**********************************************************************
- ** PUBLIC DrawWindow
- ***********************************************************************/
-
- // Draw the contents of an application window.
-
- void TRegisteredObjectsWindow::DrawWindow()
- {
- Rect tRect;
-
- SetPort(fDocWindow);
- tRect = fDocWindow->portRect;
- EraseRect(&tRect);
- InvalRect(&tRect);
- }
-
- /**********************************************************************
- ** PUBLIC UpdateList
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::UpdateList()
- {
- GrafPtr savedPort;
- GetPort(&savedPort);
- SetPort(GrafPtr(GetDocWindow()));
-
- EraseRect(&(**fList->GetList()).rView);
-
- fList->DoDraw(false); // turn off list drawing
- unsigned short count = (unsigned short)fRegisteredObjects->GetCount();
-
- // fList->DelRow(0,0); // delete the entire list
- // fList->AddRow(count); // make space for the cells
-
- //
- // adjust the number of rows in the list
- //
- if (count > fList->GetLastRow())
- fList->AddRow(count - fList->GetLastRow());
- else if (count < fList->GetLastRow())
- fList-> DelRow(fList->GetLastRow() - count, count);
-
- for (int row = 0; row < count; row++) {
- fRegisteredObjects->GetSemaphore()->Grab(); // don't let list change on us
- if (fRegisteredObjects->GetCount() > row) { // make sure list has index "row"
- char theData[256];
- (*fRegisteredObjects)[row]->GetVerboseName(theData); // get the text for the new cell
- fRegisteredObjects->GetSemaphore()->Release(); // ok, it can change now
-
- Cell cell = {0,0}; // {row,0} won't work!!!
- cell.v = row; // we'll add the cell to the end
- fList->SetCell(theData,strlen(theData),cell);
- }
- else
- fRegisteredObjects->GetSemaphore()->Release();
- }
-
- // force the list to be updated
-
- fList->DoDraw(true); // turn on list drawing
- InvalRect(&(**fList->GetList()).rView);
- SetPort(savedPort);
- }
-
- /**********************************************************************
- ** PUBLIC GetListRect
- ***********************************************************************/
-
- Rect TRegisteredObjectsWindow::GetListRect()
- {
- return fList->GetListRect();
- }
-
-
- /**********************************************************************
- ** PUBLIC DrawGrowIcon
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::DrawGrowIcon()
- {
- ::DrawGrowIcon(fDocWindow);
-
- /* get rid of extra "grow icon" section in lower left corner */
- Rect r = fDocWindow->portRect;
- r.top = r.bottom - 16;
- r.bottom -=14;
- r.right = r.left + kButtonSpace - 1;
- EraseRect(&r);
- }
-
- /**********************************************************************
- ** PUBLIC MoveWindow
- ***********************************************************************/
-
- // Move an application window.
-
- void TRegisteredObjectsWindow::MoveWindow(short hGlobal, short vGlobal)
- {
- ::MoveWindow(fDocWindow, hGlobal, vGlobal, false);
- }
-
- /**********************************************************************
- ** PUBLIC SizeWindow
- ***********************************************************************/
-
- // Set the size of an application window.
-
- void TRegisteredObjectsWindow::SizeWindow(short w, short h)
- {
- ::SizeWindow(fDocWindow, w, h, true);
- fList->DoDraw(false);
- fList->Size(w - 15 - kButtonSpace, h - 15);
- fList->DoDraw(true);
- DrawWindow();
- }
-
- /**********************************************************************
- ** PUBLIC HomeWindow
- ***********************************************************************/
-
- // Move the window to the "home" position so it can definitely be seen.
-
- void TRegisteredObjectsWindow::HomeWindow()
- {
- MoveWindow(kWindowHStart, kWindowVStart);
- SizeWindow(kWindowHSizeStart, kWindowVSizeStart);
- }
-
- /**********************************************************************
- ** PUBLIC GetWindowSize
- ***********************************************************************/
-
- // Returns size of an application window.
-
- void TRegisteredObjectsWindow::GetWindowSize(short &w, short &h)
- {
- w = fDocWindow->portRect.right - fDocWindow->portRect.left;
- h = fDocWindow->portRect.bottom - fDocWindow->portRect.top;
- }
-
- /**********************************************************************
- ** PUBLIC GetWindowLocation
- ***********************************************************************/
-
- // Returns the location of an application window.
-
- void TRegisteredObjectsWindow::GetWindowLocation(short &hGlobal, short &vGlobal)
- {
- hGlobal = - fDocWindow->portBits.bounds.left;
- vGlobal = - fDocWindow->portBits.bounds.top;
- }
-
- /**********************************************************************
- ** PUBLIC SaveWindowLocation
- ***********************************************************************/
-
- void TRegisteredObjectsWindow::SaveWindowLocation()
- {
- //
- // save the window size and location in the prefs file
- //
- short prefsRefNum = fInspector->GetPrefsFileRefNum();
- Handle windowLocationResource = NULL;
- if (prefsRefNum != -1)
- windowLocationResource = NewHandle(sizeof(WindowLocationRec));
- if (windowLocationResource)
- {
- signed short left, top;
- signed short width, height;
-
- GetWindowLocation(left, top);
- GetWindowSize(width, height);
-
- HLock(windowLocationResource);
- (**(WindowLocationRec**)windowLocationResource).left = left;
- (**(WindowLocationRec**)windowLocationResource).top = top;
- (**(WindowLocationRec**)windowLocationResource).width = width;
- (**(WindowLocationRec**)windowLocationResource).height = height;
-
- short savedRefNum = CurResFile();
- UseResFile(prefsRefNum);
- char className[255];
- strcpy(className,fRegisteredObjects->GetClassName());
- c2pstr(className);
- Handle dupRes = Get1NamedResource('wloc', (ConstStr255Param)className);
- if (dupRes)
- {
- RemoveResource(dupRes);
- DisposeHandle(dupRes);
- }
- AddResource(windowLocationResource, 'wloc', UniqueID('wloc'),
- (ConstStr255Param)className);
- WriteResource(windowLocationResource);
- UseResFile(savedRefNum);
- }
- }
-
- /**********************************************************************
- ** PUBLIC RestoreWindowLocation
- ***********************************************************************/
-
- Boolean TRegisteredObjectsWindow::RestoreWindowLocation()
- {
- Boolean result = false; // true if relocation was actually done
- short prefsRefNum = fInspector->GetPrefsFileRefNum();
-
- if (prefsRefNum)
- {
- signed short left, top;
- signed short width, height;
-
- short savedRefNum = CurResFile();
- UseResFile(prefsRefNum);
- char className[255];
- strcpy(className,fRegisteredObjects->GetClassName());
- c2pstr(className);
- Handle windowLocationResource = Get1NamedResource('wloc', (ConstStr255Param)className);
- if (windowLocationResource)
- {
- HLock(windowLocationResource);
- left = (**(WindowLocationRec**)windowLocationResource).left;
- top = (**(WindowLocationRec**)windowLocationResource).top;
- width = (**(WindowLocationRec**)windowLocationResource).width;
- height = (**(WindowLocationRec**)windowLocationResource).height;
- ReleaseResource(windowLocationResource);
-
- MoveWindow(left, top);
- SizeWindow(width, height);
-
- result = true;
- }
- UseResFile(savedRefNum);
- }
-
- return result;
- }
-